package cn.zhaozhiqi.controller.api;

import cn.zhaozhiqi.pojo.domain.ChargeRuleWait;
import cn.zhaozhiqi.pojo.domain.ProfitsharingRuleBase;
import cn.zhaozhiqi.pojo.domain.ProfitsharingRuleDeductCancel;
import cn.zhaozhiqi.pojo.domain.ProfitsharingRuleDeductComplaint;
import cn.zhaozhiqi.remote.api.IRuleFeignClient;
import cn.zhaozhiqi.remote.dto.*;
import cn.zhaozhiqi.result.JSONResult;
import cn.zhaozhiqi.service.*;
import cn.zhaozhiqi.utils.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;

@RestController
public class RuleFeignClientImpl implements IRuleFeignClient {

    @Autowired
    private IChargeRuleStartService chargeRuleStartService;
    @Autowired
    private IChargeRuleWaitService ruleWaitService;
    @Autowired
    private IProfitsharingRuleBaseService baseService;
    @Autowired
    private IProfitsharingRuleDeductCancelService cancelService;
    @Autowired
    private IProfitsharingRuleDeductComplaintService complaintService;


    /**
     * 计算根据今日司机的取消订单量和投诉量确定今日分账比例
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public JSONResult<OrderProfitsharingResultDto> getOrderProfitsharingDto(OrderProfitsharingDto dto) {
        Integer todayCancel = dto.getTodayCancel();
        Integer todayComplaint = dto.getTodayComplaint();
        //拿到最初的分账比例，再进行酌情扣减
        ProfitsharingRuleBase ruleBase = baseService.getOne(null);
        BigDecimal baseDriverRatio = ruleBase.getDriverRatio();
        //2.根据订单取消数量获取今日扣减比例
        if (todayCancel != 0) {
            ProfitsharingRuleDeductCancel cencel = cancelService.getOne(new LambdaQueryWrapper<ProfitsharingRuleDeductCancel>()
                    .lt(ProfitsharingRuleDeductCancel::getNumberFrom, todayCancel)
                    .ge(ProfitsharingRuleDeductCancel::getNumberTo, todayCancel));
            //扣减司机主动取消的惩罚比例
            baseDriverRatio = baseDriverRatio.subtract(cencel.getDriverRatioDeduct());
        }
        if(todayComplaint != 0){
            ProfitsharingRuleDeductComplaint complaint = complaintService.getOne(
                    new LambdaQueryWrapper<ProfitsharingRuleDeductComplaint>()
                    .lt(ProfitsharingRuleDeductComplaint::getNumberFrom, todayComplaint)
                    .ge(ProfitsharingRuleDeductComplaint::getNumberTo, todayComplaint));
            //扣减司机投诉
            baseDriverRatio = baseDriverRatio.subtract(complaint.getDriverRatioDeduct());
        }
        OrderProfitsharingResultDto resultDto = new OrderProfitsharingResultDto();
        resultDto.setPlatformRate(new BigDecimal(1).subtract(baseDriverRatio).setScale(2, RoundingMode.HALF_UP));
        resultDto.setDriverRate(baseDriverRatio.setScale(2, RoundingMode.HALF_UP));

        return JSONResult.success(resultDto);
    }
    
    
    

    @Override
    public JSONResult orderComputed(RuleComputedDto dto) {
        return JSONResult.success(chargeRuleStartService.orderComputed(dto));
    }

    /**
     * 计算等时费,拿取等时费计算规则的接口
     * @param dto
     * @return
     */
    @Override
    public JSONResult<WaitTimeDetailDto> waitTimeConputed(WaitTimeComputedDto dto) {
        //1.拿取等时费规则对象
        ChargeRuleWait ruleWait = ruleWaitService.getOne(null);
        AssertUtil.isNotNull(ruleWait,"通过feign拿取等时费规则有误");
        WaitTimeDetailDto detailDto = new WaitTimeDetailDto();
        dto.setWaitingMinute(dto.getWaitingMinute());
        //拿取规则准备计算
       Integer freeBaseWaitingMinute = ruleWait.getFreeBaseWaitingMinute();//免费分钟
        detailDto.setFreeBaseWaitingMinute(freeBaseWaitingMinute);
        BigDecimal execeedBaseWaitingEveryKmAmount = ruleWait.getExceedEveryMinuteAmount();//超出免费等时每分钟价格
        detailDto.setExeceedBaseWaitingEveryKmAmount(execeedBaseWaitingEveryKmAmount);
        //进行判断，根据是否超出免费等时的分钟数来进行不同的赋值。
        //1.若超出，则计算；   2.若未超出，则超出费用直接写成零
        if (dto.getWaitingMinute().intValue() > freeBaseWaitingMinute){
            //计算超出分钟数
        Integer execeedBaseWaitingMinute = dto.getWaitingMinute().intValue() - freeBaseWaitingMinute;
        //计算等时费
        BigDecimal waitingAmount = execeedBaseWaitingEveryKmAmount.multiply(new BigDecimal(execeedBaseWaitingMinute));
            detailDto.setExeceedBaseWaitingMinute(execeedBaseWaitingMinute);
            detailDto.setWaitingAmount(waitingAmount);
        }else{
            detailDto.setExeceedBaseWaitingMinute(0);
            detailDto.setWaitingAmount(BigDecimal.ZERO);
        }
        return JSONResult.success(detailDto);
    }

  
}
